package com.ocom.payhandle.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.payconfig.EntityXfZoneDate;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.utils.DateUtils;
import com.ocom.common.vo.pyhandle.ConsTimeZoneVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payhandle.dao.IDaoXfZoneDate;
import com.ocom.payhandle.service.XfZoneDateService;
import com.ocom.redis.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class XfZoneDateServiceImpl extends ServiceImpl<IDaoXfZoneDate, EntityXfZoneDate> implements XfZoneDateService {

	@Resource
	RedisUtil redisUtil;

	@Autowired
	IDaoXfZoneDate iDaoXfZoneDate;

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB)
	public ConsTimeZoneVo getTimeZone(Long companyId, Date consDate) {
		String date  = DateUtils.getStrDate(consDate,"yyyy-MM-dd");
		String canKey = "ConsTimeZone#comId:"+companyId ;
		Object	ob = redisUtil.hmGet(canKey,date);
		if(ob!=null){
			ConsTimeZoneVo consTimeZoneVo1 = (ConsTimeZoneVo) ob;
			//缓存超期
			if((consTimeZoneVo1.getTimestamp()-System.currentTimeMillis())<86400000){
				return consTimeZoneVo1;
			}
		}
		ConsTimeZoneVo consTimeZoneVo = iDaoXfZoneDate.getTimeZoneDate(companyId,date);
		if(consTimeZoneVo==null){
			Integer day = DateUtils.dateToWeek(date);
			consTimeZoneVo = iDaoXfZoneDate.getTimeZoneDay(companyId,day);
		}
		if(consTimeZoneVo==null){
			consTimeZoneVo =  new ConsTimeZoneVo();
		}
		consTimeZoneVo.setTimestamp(System.currentTimeMillis());
		redisUtil.hmSet(canKey,date,consTimeZoneVo);
		redisUtil.expire(canKey,86400l);

		return consTimeZoneVo;
	}
}
